package com.fenshi.manager.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fenshi.bean.UserBean;
import com.fenshi.manager.R;
import com.fenshi.manager.adapter.MRecyclerAdapter;
import com.fenshi.presenter.MainPresenter;
import com.fenshi.presenter.imp.OrderListPresenterImp;
import com.fenshi.view.MainView;
import com.quickdv.activity.adapter.viewholder.DataViewHolder;
import com.quickdv.activity.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 用户未还车订单列表
 */
public class OrderManagerFragment2 extends BaseFragment implements MainView {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "type";
    @InjectView(R.id.empty_view)
    TextView emptyView;

    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recycler;
    private List<Map<String, Object>> data;
    private int type;
    MRecyclerAdapter adapter;
    MainPresenter presenter;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @return A new instance of fragment OrderFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static OrderManagerFragment2 newInstance(int param1) {
        OrderManagerFragment2 fragment = new OrderManagerFragment2();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        type = getArguments().getInt(ARG_PARAM1);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    protected void initView() {
        refreshLayout = findView(R.id.order_refreshlayout);
        recycler = findView(R.id.order_list);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getOrder(true);
            }
        });
        buildData();
        orderType();

    }

    private void orderType() {
        adapter = new MRecyclerAdapter(getActivity(), data, R.layout.grid_maintenance_car2, new String[]{"carname", "username", "starttime", "status"}, new int[]{R.id.order_car_name, R.id.order_custome, R.id.order_time, R.id.order_status}) {
            @Override
            public void addClickListener(DataViewHolder holder, final int position) {
                //添加具体点击实现
                holder.getRootView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (activity != null) {
                            presenter.onItemClick(position);
                        }
                    }
                });
            }

            @Override
            public void onBindViewHolder(DataViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);

                TextView view = (TextView) holder.getView(R.id.order_status);
                if (data.get(position).get("status").equals("1")) {
                    view.setText("使用中");
                    view.setTextColor(activity.getResources().getColor(R.color.red));
                } else if (data.get(position).get("status").equals("2")) {
                    view.setText("待还车");
                    view.setTextColor(activity.getResources().getColor(R.color.red));
                }
                presenter.onImageLoad(holder,position);
            }
        };
        recycler.setAdapter(adapter);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayout.VERTICAL);
        recycler.setLayoutManager(manager);
    }


    @Override
    public void setCLickListener() {

    }


    @Override
    protected int getLayoutViewID() {
        return R.layout.activity_order_fragment_send;
    }

    private void buildData() {
        data = new ArrayList<>();
        presenter = new OrderListPresenterImp(activity, this);
        getOrder(true);
    }


    @Override
    public void showLoad() {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
            }
        });
    }

    @Override
    public void dismissLoad() {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void notifyDateChanged() {
        adapter.notifyDataSetChanged();
    }

    @Override
    public void setDate(List<Map<String, Object>> data, boolean isRefresh) {
        if (isRefresh)
            this.data.clear();
        if (data != null)
            this.data.addAll(data);

        if (this.data.size() == 0) {
            emptyView.setVisibility(View.VISIBLE);
            recycler.setVisibility(View.GONE);
        } else {
            emptyView.setVisibility(View.GONE);
            recycler.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showError(String error) {
        Toast.makeText(activity, error, Toast.LENGTH_LONG).show();
    }

    @Override
    public void initViewEvent(UserBean bean) {

    }

    protected void getOrder(boolean isRefresh) {
        presenter.getOrder("1,2", isRefresh);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
